home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-9.10-netbook-remix-PL.iso / casper / filesystem.squashfs / usr / lib / python2.6 / email / base64mime.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2009-11-11  |  5KB  |  154 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. """Base64 content transfer encoding per RFCs 2045-2047.
  5.  
  6. This module handles the content transfer encoding method defined in RFC 2045
  7. to encode arbitrary 8-bit data using the three 8-bit bytes in four 7-bit
  8. characters encoding known as Base64.
  9.  
  10. It is used in the MIME standards for email to attach images, audio, and text
  11. using some 8-bit character sets to messages.
  12.  
  13. This module provides an interface to encode and decode both headers and bodies
  14. with Base64 encoding.
  15.  
  16. RFC 2045 defines a method for including character set information in an
  17. `encoded-word' in a header.  This method is commonly used for 8-bit real names
  18. in To:, From:, Cc:, etc. fields, as well as Subject: lines.
  19.  
  20. This module does not do the line wrapping or end-of-line character conversion
  21. necessary for proper internationalized headers; it only does dumb encoding and
  22. decoding.  To deal with the various line wrapping issues, use the email.header
  23. module.
  24. """
  25. __all__ = [
  26.     'base64_len',
  27.     'body_decode',
  28.     'body_encode',
  29.     'decode',
  30.     'decodestring',
  31.     'encode',
  32.     'encodestring',
  33.     'header_encode']
  34. from binascii import b2a_base64, a2b_base64
  35. from email.utils import fix_eols
  36. CRLF = '\r\n'
  37. NL = '\n'
  38. EMPTYSTRING = ''
  39. MISC_LEN = 7
  40.  
  41. def base64_len(s):
  42.     '''Return the length of s when it is encoded with base64.'''
  43.     (groups_of_3, leftover) = divmod(len(s), 3)
  44.     n = groups_of_3 * 4
  45.     if leftover:
  46.         n += 4
  47.     
  48.     return n
  49.  
  50.  
  51. def header_encode(header, charset = 'iso-8859-1', keep_eols = False, maxlinelen = 76, eol = NL):
  52.     '''Encode a single header line with Base64 encoding in a given charset.
  53.  
  54.     Defined in RFC 2045, this Base64 encoding is identical to normal Base64
  55.     encoding, except that each line must be intelligently wrapped (respecting
  56.     the Base64 encoding), and subsequent lines must start with a space.
  57.  
  58.     charset names the character set to use to encode the header.  It defaults
  59.     to iso-8859-1.
  60.  
  61.     End-of-line characters (\\r, \\n, \\r\\n) will be automatically converted
  62.     to the canonical email line separator \\r\\n unless the keep_eols
  63.     parameter is True (the default is False).
  64.  
  65.     Each line of the header will be terminated in the value of eol, which
  66.     defaults to "\\n".  Set this to "\\r\\n" if you are using the result of
  67.     this function directly in email.
  68.  
  69.     The resulting string will be in the form:
  70.  
  71.     "=?charset?b?WW/5ciBtYXp66XLrIHf8eiBhIGhhbXBzdGHuciBBIFlv+XIgbWF6euly?=\\n
  72.       =?charset?b?6yB3/HogYSBoYW1wc3Rh7nIgQkMgWW/5ciBtYXp66XLrIHf8eiBhIGhh?="
  73.  
  74.     with each line wrapped at, at most, maxlinelen characters (defaults to 76
  75.     characters).
  76.     '''
  77.     if not header:
  78.         return header
  79.     if not keep_eols:
  80.         header = fix_eols(header)
  81.     
  82.     base64ed = []
  83.     max_encoded = maxlinelen - len(charset) - MISC_LEN
  84.     max_unencoded = max_encoded * 3 // 4
  85.     for i in range(0, len(header), max_unencoded):
  86.         base64ed.append(b2a_base64(header[i:i + max_unencoded]))
  87.     
  88.     lines = []
  89.     for line in base64ed:
  90.         if line.endswith(NL):
  91.             line = line[:-1]
  92.         
  93.         lines.append('=?%s?b?%s?=' % (charset, line))
  94.     
  95.     joiner = eol + ' '
  96.     return joiner.join(lines)
  97.  
  98.  
  99. def encode(s, binary = True, maxlinelen = 76, eol = NL):
  100.     '''Encode a string with base64.
  101.  
  102.     Each line will be wrapped at, at most, maxlinelen characters (defaults to
  103.     76 characters).
  104.  
  105.     If binary is False, end-of-line characters will be converted to the
  106.     canonical email end-of-line sequence \\r\\n.  Otherwise they will be left
  107.     verbatim (this is the default).
  108.  
  109.     Each line of encoded text will end with eol, which defaults to "\\n".  Set
  110.     this to "\r
  111. " if you will be using the result of this function directly
  112.     in an email.
  113.     '''
  114.     if not s:
  115.         return s
  116.     if not binary:
  117.         s = fix_eols(s)
  118.     
  119.     encvec = []
  120.     max_unencoded = maxlinelen * 3 // 4
  121.     for i in range(0, len(s), max_unencoded):
  122.         enc = b2a_base64(s[i:i + max_unencoded])
  123.         if enc.endswith(NL) and eol != NL:
  124.             enc = enc[:-1] + eol
  125.         
  126.         encvec.append(enc)
  127.     
  128.     return EMPTYSTRING.join(encvec)
  129.  
  130. body_encode = encode
  131. encodestring = encode
  132.  
  133. def decode(s, convert_eols = None):
  134.     '''Decode a raw base64 string.
  135.  
  136.     If convert_eols is set to a string value, all canonical email linefeeds,
  137.     e.g. "\\r\\n", in the decoded text will be converted to the value of
  138.     convert_eols.  os.linesep is a good choice for convert_eols if you are
  139.     decoding a text attachment.
  140.  
  141.     This function does not parse a full MIME header value encoded with
  142.     base64 (like =?iso-8895-1?b?bmloISBuaWgh?=) -- please use the high
  143.     level email.header class for that functionality.
  144.     '''
  145.     if not s:
  146.         return s
  147.     dec = a2b_base64(s)
  148.     if convert_eols:
  149.         return dec.replace(CRLF, convert_eols)
  150.     return dec
  151.  
  152. body_decode = decode
  153. decodestring = decode
  154.